/**
 * Project: cherokee.web
 * 
 * File Created at 2013-11-14
 * $Id$
 * 
 * Copyright 2011 soucheke.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Soucheke Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with soucheke.com.
 */
package cn.dface.ameba.toolkit.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;
import org.springframework.web.util.WebUtils;

/**
 * SessionUtils
 * 
 * @author dh
 */
public class SessionUtils {

    public static String USER        = "user";
    public static String TICKET       = "__ticket__";
    public static String CONTEXT_PATH = "/";
    
    /**
     * 获取请求中的token
     * 
     * @param request
     * @return
     */
    public static String getTicketFromRequest(HttpServletRequest request) {
        String token = getCookieValue(request, TICKET);
        if (StringUtils.hasText(token)) {
            return token;
        }
        return request.getParameter("ticket");
    }
    

    /**
     * 根据token获取session中的值
     * 
     * @param request
     * @return
     */
    public static Object getSessionValue(HttpServletRequest request, String key) {
        return WebUtils.getSessionAttribute(request, key);
    }

    /**
     * session中添加token对象
     * 
     * @param request
     * @param obj
     */
    public static void setSessionValue(HttpServletRequest request, String key, Object obj) {
        WebUtils.setSessionAttribute(request, key, obj);
    }

    public static String getCookieValue(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(cookieName)) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
    
    public static void addCookieValue(HttpServletResponse response, String key, String value) {
        // 写入token
        Cookie cookie = new Cookie(key, value);
        cookie.setPath(CONTEXT_PATH);
        cookie.setMaxAge(7 * 24 * 60 * 60);
        response.addCookie(cookie);
    }

    /**
     * 清除cookie
     * 
     * @param cookieName
     * @param response
     * @param path
     */
    public static void removeCookie(String cookieName, HttpServletResponse response, String path) {
        Cookie cookie = new Cookie(cookieName, null);
        cookie.setMaxAge(0);
        if (path == null) {
            cookie.setPath(CONTEXT_PATH);
        } else {
            cookie.setPath(path);
        }
        response.addCookie(cookie);
    }

}
